#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=..\..\Quake III Arena\quake3.ico
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Res_Comment=Installer for quake3:// URL handler to launch Quake3
#AutoIt3Wrapper_Res_Description=Installer for quake3:// URL handler to launch Quake3
#AutoIt3Wrapper_Res_Fileversion=1.2.2.2
#AutoIt3Wrapper_Res_LegalCopyright=(c) 2012 by Dave Fulgham
#AutoIt3Wrapper_Res_Language=1033
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:  Dave Fulgham

 Script Function:
	Installer for quake3://  URL handler to launch Quake3.

#ce ----------------------------------------------------------------------------
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

FileInstall("background.gif", @TempDir & "\background.gif")

Opt("MustDeclareVars", 1)

Dim $regRead, $serverIP, $quakePath, $quakePort
Dim $Form1, $btnBrowse, $btnSave, $iQuakePath, $iQuakePort, $Label1, $Label2
Dim $Pic1, $changePort, $nMsg, $ourResult
Enum $getDataCancel, $getDataSave

;MsgBox(0, "debug", "Running...")

If Not StringInStr(@ScriptFullPath, @UserProfileDir & "\" & @ScriptName) Then
	FileCopy(@ScriptFullPath, @UserProfileDir & "\" & @ScriptName, 1)
EndIf

$regRead = RegRead("HKEY_CURRENT_USER\Software\Classes\quake3", "")
If @error Then
	;MsgBox(0, "debug", "About to write")
	RegWrite("HKEY_CURRENT_USER\Software\Classes\quake3", "", "REG_SZ", "URL:Quake3 Protocol")
	;MsgBox(0, "debug", @error)
	RegWrite("HKEY_CURRENT_USER\Software\Classes\quake3", "URL Protocol", "REG_SZ", "")
	RegWrite("HKEY_CURRENT_USER\Software\Classes\quake3\Default Icon", "", "REG_SZ", "quake3.exe,1")
	RegWrite("HKEY_CURRENT_USER\Software\Classes\quake3\shell\open\command", "", "REG_SZ", '"' & @UserProfileDir & "\" & @ScriptName & '" "%1"')
EndIf

$quakePath = RegRead("HKCU\software\ditch.org", "Q3Path")
If (@error Or $CmdLine[0] = 0) Then
	;$quakePath = FileOpenDialog("Please Browse for your Quake3 Executable", @DesktopDir, "Program Files (*.exe)", "quake3.exe")
	;If $quakePath = "" Then Exit
	;RegWrite("HKCU\software\ditch.org", "Q3Path", "REG_SZ", $quakePath)
	$ourResult = GetData()
	If $ourResult = $getDataCancel Then Exit
	$quakePath = RegRead("HKCU\software\ditch.org", "Q3Path")
	If @error Then
		MsgBox(16, "Error", "Unable to determine the path to Quake3.exe")
		Exit
	EndIf
EndIf

;MsgBox(0, "debug", $CmdLine[0])
If $CmdLine[0] > 0 Then
	$serverIP = StringRegExp($CmdLine[1], "/([0-9.:]+)/?$", 1)
	;MsgBox(0, "debug", $serverIP[0])
	Local $quakeFolder = StringRegExpReplace($quakePath, "\\(\w+\.[Ee][Xx][Ee])$", "")
	;MsgBox(0, "debug", $quakeFolder)
	Local $ourPort = RegRead("HKCU\software\ditch.org", "Q3Port")
	If @error Then
		MsgBox(64, "Info", "Couldn't read Quake3 port from registry, using default port of 27960")
		$ourPort = "27960"
	EndIf
	Run($quakePath & " +connect " & $serverIP[0] & " +seta cl_allowDownload 1 +set net_port " & $ourPort, $quakeFolder)
EndIf

Func GetData()
	#Region ### START Koda GUI section ### Form=
	$Form1 = GUICreate("Quake III URL Launcher", 552, 296, 192, 124)
	$Pic1 = GUICtrlCreatePic(@TempDir & "\background.gif", 0, 0, 550, 200)
	$Label1 = GUICtrlCreateLabel("Quake III EXE Path:", 10, 203, 122, 24)
	GUICtrlSetFont($Label1, 12, 400, 0, "Impact")
	$iQuakePath = GUICtrlCreateInput("", 8, 227, 457, 21)
	GUICtrlSetTip($iQuakePath, "This is the path to your Quake3 executable.")
	$quakePath = RegRead("HKCU\software\ditch.org", "Q3Path")
	GUICtrlSetData($iQuakePath, $quakePath)
	$btnBrowse = GUICtrlCreateButton("Browse", 472, 224, 65, 25)
	GUICtrlSetFont($btnBrowse, 10, 400, 0, "Impact")
	$iQuakePort = GUICtrlCreateInput("27960", 368, 256, 97, 25, BitOR($GUI_SS_DEFAULT_INPUT,$ES_RIGHT,$ES_OEMCONVERT,$ES_READONLY))
	GUICtrlSetFont($iQuakePort, 10, 400, 0, "Impact")
	$changePort = GUICtrlCreateUpdown($iQuakePort, 0x80)
	GUICtrlSetTip($iQuakePort, "Change this if you experience Game Challenge issues.")
	GUICtrlSetTip($changePort, "Change this if you experience Game Challenge issues.")
	$quakePort = RegRead("HKCU\software\ditch.org", "Q3Port")
	If StringIsInt($quakePort) Then
		GUICtrlSetData($iQuakePort, $quakePort)
	EndIf
	$btnSave = GUICtrlCreateButton("Save", 472, 256, 65, 25)
	GUICtrlSetFont($btnSave, 10, 400, 0, "Impact")
	$Label2 = GUICtrlCreateLabel("Port:", 333, 255, 34, 24)
	GUICtrlSetFont($Label2, 12, 400, 0, "Impact")
	GUISetState(@SW_SHOW)
	#EndRegion ### END Koda GUI section ###

	While 1
		$nMsg = GUIGetMsg()
		Switch $nMsg
			Case $GUI_EVENT_CLOSE
				;Exit
				GUIDelete($Form1)
				Return $getDataCancel
			Case $btnSave
				RegWrite("HKCU\software\ditch.org", "Q3Path", "REG_SZ", $quakePath)
				RegWrite("HKCU\software\ditch.org", "Q3Port", "REG_SZ", GUICtrlRead($iQuakePort))
				Return $getDataSave
			Case $btnBrowse
				$quakePath = FileOpenDialog("Please Browse for your Quake3 Executable", @DesktopDir, "Program Files (*.exe)", "quake3.exe")
				GUICtrlSetData($iQuakePath, $quakePath)
				;If $quakePath = "" Then GUIDelete($Form1)
		EndSwitch
	WEnd
EndFunc

